package servlet;//package com.example.classhoursystem.servlet;
//
//import com.example.classhoursystem.dao.StudentInfoDAO;
//import org.apache.commons.fileupload.FileItem;
//import org.apache.commons.fileupload.disk.DiskFileItemFactory;
//import org.apache.commons.fileupload.servlet.ServletFileUpload;
//
//
//import javax.servlet.ServletException;
//import javax.servlet.annotation.WebServlet;
//import javax.servlet.http.HttpServlet;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.File;
//import java.io.IOException;
//import java.util.List;
//import java.util.UUID;
//
//
//@WebServlet(name = "RegistUserServlet", value = "/RegistUserServlet")
//public class RegistUserServlet extends HttpServlet {
//    @Override
//    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        request.setCharacterEncoding("utf-8");
//        response.setContentType("text/html;charset=UTF-8");
//        try {
//            DiskFileItemFactory factory = new DiskFileItemFactory();
//            ServletFileUpload fileupload = new ServletFileUpload(factory);
//            fileupload.setHeaderEncoding("utf-8");
//            List<FileItem> fileitems = fileupload.parseRequest(request);
//            User user=new User();
//            for (FileItem fileitem : fileitems) {
//                // 判断是否为普通字段
//                if (fileitem.isFormField()) {
//                    // 获得字段名和字段值
//                    String name = fileitem.getFieldName();
//                    if ("number".equals(name)) {
//                            String value = fileitem.getString("utf-8");
//                       user.setNumber(value);
//                    } else  if ("username".equals(name)) {
//                        String value = fileitem.getString("utf-8");
//                        user.setUsername(value);
//                    } else if ("realname".equals(name)) {
//                        String value = fileitem.getString("utf-8");
//                        user.setRealname(value);
//                    } else if ("password".equals(name)) {
//                        String value = fileitem.getString("utf-8");
//                        user.setPassword(value);
//                    } else if ("role".equals(name)) {
//                        String value = fileitem.getString("utf-8");
//                        user.setRole(value);
//                    }
//                } else {// 这是上传输入项
//                    String filename = fileitem.getName();// 获取上传的文件名
//                    // 截取出文件名
//                    filename = filename.substring(filename.lastIndexOf("\\") + 1);
//                    filename = UUID.randomUUID().toString() + "_" + filename;
//                    user.setHead(filename);
//                    // 将服务器中文件夹路径与文件名组合成完整的服务器端路径
//                    String filepath = getServletContext().getRealPath("/upload/");
//                    File file = new File(filepath,filename);
//                    file.getParentFile().mkdirs();
//                    fileitem.write(file);
//                }
//            }
//            System.out.println("user = " + user);
//            UserDao userDao=new UserDao();
//            userDao.regist(user);
//            request.getSession().setAttribute("msg","用户注册成功");
//            response.sendRedirect(request.getContextPath()+"/index.jsp");
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//    }
//
//    @Override
//    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//       doGet(request,response);
//    }
//}
